<#include "/includes/vars.inc"> 
<@pp.dropOutputFile />
<#list entities.entity as entity>
	<#if entity.@singleton="true">
    <#assign entityName = entity.@name>
    <@pp.changeOutputFile name="${javaDir}${packageDir}/web/"+entityName?cap_first+"ControllerBase.java" />
   	
package ${project.@rootPackage}.web;

import ${project.@rootPackage}.domain.${entityName};
import ${project.@rootPackage}.service.${entityName}Service;
    <#list entity["many-to-one"] as rel>
		<#if rel.@entity = config.login.@entity>
import ${project.@rootPackage}.web.support.LoginController;
        </#if>
    </#list>
import ${project.@rootPackage}.web.util.JsfUtil;
<#list entity.column.enum as enum>
import ${project.@rootPackage}.domain.enums.${enum?parent.@type};
</#list> 
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Controller class for entity ${entityName}
 * <#if project.@author[0]??>@author ${project.@author}</#if>
 */
public abstract class ${entityName}ControllerBase {

	protected static final Logger logger = Logger.getLogger("${entityName}Controller");

    <#list entity["many-to-one"] as rel>
    	<#if rel.@entity != entityName>
    @Autowired
    		<#if rel.@entity = config.login.@entity>
    protected LoginController loginController;
     		<#else>
    protected ${rel.@entity}Controller ${rel.@entity?uncap_first}Controller;
        	</#if>
        </#if>
    </#list>
     <#list entity["many-to-many"] as rel>
        <#if !rel.@mappedBy[0]??>
    @Autowired
    protected ${rel.@entity}Controller ${rel.@entity?uncap_first}Controller;
        </#if>
    </#list>
    @Autowired
    protected ${entityName}Service service;
    protected ${entityName} current;
    
    public ${entityName}ControllerBase() {
        //current=new ${entityName}();
    }
    
     @PostConstruct
    public void init(){
        //Create admin current if table currents is empty
        if (service.findAll().isEmpty()){
            ${entityName} current = new ${entityName}();
            if (service.create(current)){
                 System.out.println("${entityName} table is empty, singleton (unique) row created");
            }            
        }
    }

    public void save() {
    <#list entity["many-to-one"] as rel>
        <#if rel.@required[0]="true" && rel.@mode[0]="AUTO">
        	 <#if rel.@entity = config.login.@entity>
       	current.set${rel.@name?cap_first}(loginController.get${config.login.@entity}());
        	 <#else>
        current.set${rel.@name?cap_first}(${rel.@entity?uncap_first}Controller.getCurrent());
        	</#if>
        </#if>
    </#list>
     <#list entity["one-to-one"] as rel>
        <#if rel.@required[0]="true" && rel.@mode[0]="AUTO">
        current.set${rel.@name?cap_first}(${rel.@entity?uncap_first}Controller.getCurrent());
        </#if>
    </#list>
        if (current.isPersisted()) {
            if (service.update(current)) {
                JsfUtil.addSuccessMessage(JsfUtil.getMessage("${entityName?uncap_first}Updated"));
            }
        } else {
            if (service.create(current)) {
                JsfUtil.addSuccessMessage(JsfUtil.getMessage("${entityName?uncap_first}Created"));
            }
        }
    }
 	
    public void refreshCurrent() {
        current = service.findById(1l);
        if (current == null){
        	current = new ${entityName}();
        }
    }

    public ${entityName} get${entityName}() {
        return current;
    }

    public void set${entityName}(${entityName} current) {
        this.current = current;
    }

    public void setService(${entityName}Service service) {
        this.service = service;
    }

    public void setCurrent(${entityName} current) {
        this.current = current;
    }

    public ${entityName}Service getService(){
        return service;
    }
    
    public ${entityName} getCurrent() {
    	//get the single entity, if is null create a new entity
    	if (current==null){
    		refreshCurrent();
    	}
        return current;
    }
 
}
	</#if>
</#list>
